<template>
  <basic-container>
    <el-form ref="queryForm"
             :model="queryParams"
             :inline="true"
             label-width="80px">
      <el-form-item label="操作功能"
                    prop="searchParams.search_eq_businessType">
        <el-input v-model="queryParams.searchParams.search_eq_businessType"
                  placeholder="请输入操作功能" />
      </el-form-item>
      <el-form-item label="操作人员"
                    prop="searchParams.search_eq_operName">
        <el-input v-model="queryParams.searchParams.search_eq_operName"
                  placeholder="请输入操作人员" />
      </el-form-item>
      <el-form-item label="状态"
                    prop="searchParams.search_eq_status">
        <el-select v-model="queryParams.searchParams.search_eq_status" placeholder="请选择">
          <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="操作时间"
                    prop="searchParams.search_dt_between_operTime">
        <el-date-picker
                v-model="queryParams.searchParams.search_dt_between_operTime"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions2">
        </el-date-picker>

      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"
                   size="mini"
                   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10"
            class="mb8">
      <el-col :span="1.5">
        <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="handleDeleteAll"
        >清空
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="warning"
                icon="el-icon-download"
                size="mini"
        >导出
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading"
              :data="userList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="操作模块" align="center" prop="title"/>
      <el-table-column label="功能" align="center" prop="businessType" width="80"/>
      <el-table-column label="访问地址" align="center" prop="operUrl"/>
      <el-table-column label="请求方式" align="center" prop="requestMethod" width="80"/>
      <el-table-column label="操作人" align="center" prop="operName"/>
      <el-table-column label="操作IP" align="center" prop="operIp"/>
      <el-table-column label="操作地点" align="center" prop="operLocation"/>
      <el-table-column label="操作状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作时间" align="center" :formatter="dFormat"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="small"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleInfo(scope.row)"
          >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或编辑收费员信息对话框 -->
    <el-dialog :title="title"
               :visible.sync="open"
               width="40%"
               append-to-body
               :close-on-click-modal="false">
      <el-form ref="form"
               :model="form"
               label-width="80px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="操作模块"
                          prop="name">
              <el-input v-model="form.title"
                        placeholder="请输入角色名称" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="访问地址"
                          prop="name">
              <el-input v-model="form.operUrl"
                        placeholder="请输入角色名称" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="登录地址"
                          prop="name">
              <el-input v-model="form.operLocation"
                        placeholder="请输入角色名称" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作方式"
                          prop="name">
              <el-input v-model="form.requestMethod"
                        placeholder="请输入角色名称" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="操作方法"
                      prop="name">
          <el-input v-model="form.method"
                    placeholder="请输入角色名称" disabled/>
        </el-form-item>
        <el-form-item label="请求参数"
                      prop="name">
          <el-input v-model="form.operParam"
                    type="textarea"
                    placeholder="请输入角色名称" disabled/>
        </el-form-item>
        <el-form-item label="返回参数"
                      prop="name">
          <el-input v-model="form.jsonResult"
                    type="textarea"
                    placeholder="请输入角色名称" disabled/>
        </el-form-item>
        <el-form-item label="操作时间"
                      prop="name">
          <el-input v-model="form.operTime"
                    placeholder="请输入角色名称" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </basic-container>
</template>

<script>
  import {
    listOperate,
    delOperate,
    delAllOperate,
  } from "@/api/monitor/operate";
  import {dateFormat} from '@/util/date'

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 用户信息表格数据
        userList: [],
        // 是否显示弹出层
        open: false,
        statusOptions: [],
        form:{},
        title: "详细信息",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          searchParams:{
            search_eq_businessType: undefined,
            search_eq_operName: undefined,
            search_eq_status: undefined,
            search_dt_between_operTime: undefined,
          }
        },
        pickerOptions2: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }

    },
    created() {
      this.getList();
      this.getDicts("sys_operate").then(response=>{
        this.statusOptions =response.data.data;
      });
    },
    methods: {
      /** 搜索按钮操作 */
      handleQuery () {
        for (let key in this.queryParams.searchParams) {
          if(this.queryParams.searchParams[key]!=undefined){
            if (this.queryParams.searchParams[key] == "") {
              this.queryParams.searchParams[key] = undefined;
              continue;
            }
            if(this.queryParams.searchParams[key].length>0){
              if(!this.queryParams.searchParams[key].constructor == Array){
                if(!(this.queryParams.searchParams[key].replace(" ", "").trim().length > 0)){
                  this.queryParams.searchParams[key] = undefined;
                  continue;
                }
              }
            }
          }
        }
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery () {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          title: undefined,
          operUrl: undefined,
          operLocation: undefined,
          requestMethod: undefined,
          method: undefined,
          operParam:undefined,
          jsonResult: undefined,
          status: undefined,
          operTime: undefined,
        };
        this.resetForm("form");
      },
      /** 查询收费员信息列表 */
      getList() {
        this.loading = true;
        listOperate(this.queryParams).then(response => {
          const data = response.data;
          if (response.status===200&&data.code === "00") {
            this.userList = data.data.rows;
            this.total = data.data.total;
          } else {
            this.msgError(data.msg);
          }
          this.loading = false;
        });
      },
      handleInfo(row){
        this.form = JSON.parse(JSON.stringify(row));
        this.form.operTime=dateFormat(new Date(this.form.operTime));
        this.open = true;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        // this.ids = selection.map(item => item.id)
        this.ids = selection;
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.id || this.ids;
        let tt='';
        if(row.id==undefined){
          tt='是否确认删除【' + this.ids.length + '】条数据吗?';
        }else{
          tt='是否确认删除为"' + row.name + '"的数据项?';
        }
        this.$confirm(tt, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delOperate(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 删除按钮操作 */
      handleDeleteAll() {
        this.$confirm('是否确认清空所以的数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delAllOperate();
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      dFormat(row) {
        if (row.operTime == null) {
          return "/";
        }
        return dateFormat(new Date(row.operTime));
      },
      statusFormat(row) {
        if (row.status == null) {
          return "/";
        }
        return this.selectDictLabel(this.statusOptions,row.status);
      }
    }
  };
</script>

<style>

</style>
